package bean;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.catalina.connector.Request;

//@WebServlet("/member_list")
public class MemberServlet extends HttpServlet {
//	private static final long serialVersionUID =1L;
	Connection conn;
	ArrayList<MemberVo> al;//jstl type
	StringBuilder sb;//request type
	
	public MemberServlet() {
		super();
		try {
			conn= new MyOracle1().getConn();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		// TODO Auto-generated method stub
		super.doGet(req, resp);
	}

	@Override
	protected void doPost(HttpServletRequest request, HttpServletResponse resp)
			throws ServletException, IOException {
		resp.setContentType("text/html");
		request.setCharacterEncoding("utf-8");
		resp.setCharacterEncoding("utf-8");
		
//		String rno = req.getParameter("rno");
//		String mid = req.getParameter("mid");

		
		PrintWriter out = resp.getWriter();
		
		String find = request.getParameter("find");
		String sql = "select * from member where mid like ? or irum like ?";
		PreparedStatement pst = null;
		ResultSet rs = null;
		try {
			pst = conn.prepareStatement(sql);
			pst.setString(1, "%"+ find + "%");
			pst.setString(2, "%"+ find + "%");
			rs = pst.executeQuery();
//			while(rs.next()){
//				out.print("<li>RNO=" + rs.getString("rno")+"--"+ "<li>MID=" + rs.getString("mid"));
//			}
			sb = new StringBuilder();
			al = new ArrayList<MemberVo>();
			
			while(rs.next()){
				String temp = new String().format("<li>%s[%s]", rs.getString("mid"), rs.getString("irum"));
				sb.append(temp);
				
				MemberVo vo = new MemberVo();
				vo.setMid(rs.getString("mid"));
				vo.setIrum(rs.getString("irum"));
				al.add(vo);
			}
			request.setAttribute("sb", sb);
			request.setAttribute("al", al);
			ServletContext context = getServletContext();
			RequestDispatcher dispatcher = 
					context.getRequestDispatcher("/servlet/member_list.jsp");
			
			dispatcher.forward(request, resp);
			
			rs.close();
			pst.close();
			out.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
		
	}

}
